package com.example.zhaohang.cachemanger;

import android.os.SystemClock;

import com.example.zhaohang.employmentapp.MyApplayCation;
import com.example.zhaohang.utils.ToastMake;
import com.example.zhaohang.utils.Utils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.io.File;
import java.io.IOException;

import okhttp3.Call;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;


/**
 * Created by 赵航 on 2018/1/16.
 */

public class HttpManger {

    private static final HttpManger ourInstance = new HttpManger();
    private String mString;
    private String mS;
    private String mRespone;

    private HttpManger() {

    }

    public static HttpManger getInstance() {
        return ourInstance;
    }

    /**
     * @param url
     * @return 纯get请求
     */
    public String getData(String url) {

        try {
            OkHttpClient okHttpClient = new OkHttpClient.Builder().build();
            //同步请求
            Request request = new Request.Builder()
                    .url(url)
                    .build();

            Response response = okHttpClient.newCall(request).execute();

            String string = response.body().string();

            return string;
        } catch (IOException e) {
            e.printStackTrace();
            return null;//错误直接但会null、

        }
    }


    /**
     * @param url
     * @return 包含账号跟密码的get请求
     */
    public String getUserData(String url) {

        if (MyApplayCation.appKey == null || MyApplayCation.appKey .isEmpty()) {
            return null;
        }



//        OkHttpUtils
//                .get()
//                .url(url)
//                .addParams("username", WangWangFindShip.shipMasterName)
//                .addParams("password", WangWangFindShip.shipMasterpassworld)
//                .build()
//                .execute(new StringCallback() {
//                    @Override
//                    public void onError(Call call, Exception e, int id) {
//
//                    }
//
//                    @Override
//                    public void onResponse(String response, int id) {
//                        mString = response;
//                    }
//                });

        return mString;
    }

    /**
     * @param url
     * @return 返回请求成功的字符串
     */
    public String postUserData(String url) {

//
//        if (WangWangFindShip.shipMasterName == null || WangWangFindShip.shipMasterName.isEmpty()) {
//            return null;
//        }
//
//        if (WangWangFindShip.shipMasterpassworld == null || WangWangFindShip.shipMasterpassworld.isEmpty()) {
//
//            return null;
//        }
//        OkHttpUtils
//                .post()
//                .url(url)
//                .addParams("username", WangWangFindShip.shipMasterName)
//                .addParams("password", WangWangFindShip.shipMasterpassworld)
//                .build()
//                .execute(new StringCallback() {
//                    @Override
//                    public void onError(Call call, Exception e, int id) {
//
//                    }
//
//                    @Override
//                    public void onResponse(String response, int id) {
//                        mString = response;
//                    }
//                });

        return mString;
    }

    /**
     * @param url
     * @param file 向服务器传送图片
     */
    public String postToServce(String url, File file) {


        OkHttpUtils
                .postFile()
                .url(url)
                .file(file)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(String response, int id) {
                        mString = response;

                    }
                });

        return mString;

    }

    /**
     * @param url    传入验证地址
     * @param number 传入验证电话
     * @return 返回正确的结果
     */
    public String getRegistNumber(String url, String number, final String text) {
        OkHttpUtils
                .postString()
                .url(url)
                .content(number)
                .mediaType(MediaType.parse("application/json; charset=utf-8"))
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {

                        Utils.runOnUIThread(new Runnable() {
                            @Override
                            public void run() {
                                ToastMake.showToast(text);
                            }
                        });

                    }

                    @Override
                    public void onResponse(String response, int id) {
                        mRespone = response;
                    }
                });

        SystemClock.sleep(3000);
        return mRespone;

    }

    /**
     * @param url    传入地址
     * @param number 传入Map集合的转化为json的字符串
     * @return 返回json字符串
     */
    public String postNeedNumber(String url, String number) {
        OkHttpUtils
                .postString()
                .url(url)
                .content(number)
                .mediaType(MediaType.parse("application/json; charset=utf-8"))
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
//
//                        Utils.runOnUIThread(new Runnable() {
//                            @Override
//                            public void run() {
//                                ToastMake.showToast("网络请求失败，请重新尝试");
//                            }
//                        });
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        mRespone = response;

                    }
                });
        SystemClock.sleep(1000);
        return mRespone;

    }


}
